package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.entity.YddCateData;
import com.itheima.entity.YddCate;
import com.itheima.service.YddCateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ydd_cate")
@Slf4j
public class YddCateController {
    @Autowired
    private YddCateService yddCateService;

    /**
     * 添加分类
     * @param yddCate
     * @return
     */
    @PostMapping("/add")
    public R<String>save(@RequestBody YddCate yddCate){
        yddCate.setDefaultData("1");
        yddCateService.save(yddCate);
        return R.success("添加成功");
    }

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param yddCate
     * @return
     */
    @GetMapping("/list")
    public R<YddCateData>list(String page, String limit, YddCate yddCate){
        int page1 = Integer.parseInt(page);
        int limit1 = Integer.parseInt(limit);
        Page<YddCate>pageInfo=new Page<>(page1,limit1);
        LambdaQueryWrapper<YddCate>lqw=new LambdaQueryWrapper<>();
        lqw.eq(YddCate::getDefaultData,1);
        lqw.like(yddCate.getCatename()!=null,YddCate::getCatename,yddCate.getCatename());
        Page<YddCate> page2 = yddCateService.page(pageInfo, lqw);
        //int count = yddCateService.count();
        YddCateData yddCateData=new YddCateData();
        //设置总数据条数
        yddCateData.setCount((int) page2.getTotal());
        yddCateData.setTotalPages((int)pageInfo.getPages());
        yddCateData.setPageSize((int) page2.getSize());
        Integer current = Math.toIntExact(pageInfo.getCurrent());
        //设置当前页
        yddCateData.setCurrentPage(current);
        //设置分页数据:records
        yddCateData.setData(pageInfo.getRecords());
        return R.success(yddCateData);

    }

    /**
     * 修改数据回显
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R<YddCate>rowInfo(String id){
        YddCate yddCate = yddCateService.getById(id);
        return R.success(yddCate);
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R<String>delete(String id){
        yddCateService.removeById(id);
        return R.success("删除成功");
    }

    /**
     * 修改分类
     * @param yddCate
     * @return
     */
    @PostMapping("/update")
    public R<String>update(@RequestBody YddCate yddCate){
            yddCateService.updateById(yddCate);
            return R.success("修改成功");
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/all")
    public R<List<YddCate>>all(){
        LambdaQueryWrapper<YddCate>lqw=new LambdaQueryWrapper<>();
        lqw.orderByDesc(YddCate::getSortNum);
        List<YddCate> list = yddCateService.list(lqw);
        return R.success(list);

    }
}
